
import numpy as np
import matplotlib.pyplot as plt


# 调整坐标轴：坐标轴移到原点(0,0)；坐标轴刻度数字间隔1；刻度之间的像素一样
def adjustAxis():
    # 创建图形和坐标轴对象
    fig, ax = plt.subplots()

    # 设置Y轴在0位置
    ax.spines['left'].set_position('zero')
    # 设置X轴在0位置
    ax.spines['bottom'].set_position('zero')

    # 隐藏右边和上边的坐标轴线
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')

    # 设置X、Y轴刻度间隔为1
    ax.xaxis.set_major_locator(plt.MultipleLocator(1))
    ax.yaxis.set_major_locator(plt.MultipleLocator(1))

    # 调整图形的大小,保持宽高大小一样，才能保证X轴和Y轴刻度之间的像素间隔相等
    fig.set_size_inches(5, 5)  # 根据需要调整图形的大小

    # 这个步骤是画网格线
    plt.grid(True, linestyle="--", alpha=0.5)
    # Howard 2023-09-05 增加：保持XY坐标的间距大小一样
    ax.set_aspect('equal')


# 利用 matplotlib 来进行画图
def drawXY():
    adjustAxis()

    # param:起点，终点，间距
    # x1 = np.linspace(0.1, 10, 100)  #
    # x2 = -np.linspace(0.1, 10, 100)
    # x3=np.linspace(-10, 10, 100)
    # #y = -np.abs(x) / (x ** 2 + 1)
    # y1=x1+1/x1
    # y2=x2+1/x2
    # y3=x3
    # plt.plot(x1, y1)
    # plt.plot(x2, y2)
    # plt.plot(x3, y3)

    # a=2
    # x=np.linspace(-1, 2, 100)  #
    # y=a**x
    # plt.plot(x, y,'r')
    #
    # a=3
    # x2=np.linspace(-1, 2, 100)  #
    # y2=a**x2
    # plt.plot(x2, y2,'g')

    # a=1/2
    # x=np.linspace(0.1, 10, 100)  #
    # y=x**a
    # plt.plot(x, y,'r')

    # a=0.2
    # x2=np.linspace(-1, 10, 100)  #
    # y2=a**x2
    # plt.plot(x2, y2,'g')

    x1 = np.linspace(0.1, 10, 100)  #
    y1 = np.log2(x1)
    plt.plot(x1, y1, 'g')

    x = np.linspace(0.1, 10, 100)  #
    y=np.log10(x)
    plt.plot(x, y, 'r')
    plt.show()


if __name__ == '__main__':
    drawXY()